package com.jbookshelf.beans;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import org.primefaces.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.jbookshelf.dal.entities.Authors;
import com.jbookshelf.dal.providers.AuthorsDataProvider;

@SuppressWarnings( "ALL" )
@Component
@Scope("request")
public class AuthorsBean {

	private Authors newdAuthor;
	@Autowired
	AuthorsDataProvider dataProvider;

	private List<Authors> authors;
	private Authors selectedAuthor;

	public AuthorsBean() {
		newdAuthor = new Authors();
	}

	public List<Authors> getAuthors() {
		if (authors == null) {
			authors = dataProvider.getAuthors();
		}
		return authors;
	}

	public void setAuthors(List<Authors> authors) {
		this.authors = authors;
	}

	public Authors getSelectedAuthor() {
		return selectedAuthor;
	}

	public void setSelectedAuthor(Authors selectedAuthor) {
		this.selectedAuthor = selectedAuthor;
	}

	public String getAuthorName() {
		return newdAuthor.getFullName();
	}

	public void seetAuthorName(String name) {
		newdAuthor.setFullName(name);
	}

	public String getAuthorEmail() {
		return newdAuthor.getEmail();
	}

	public Date getAuthorBirthday() {
		return newdAuthor.getBirthday();
	}

	public void showCreateDialog() {
		final RequestContext context = RequestContext.getCurrentInstance();
		context.execute("createdlg.show()");
		
		
	}

	public void createAuthor() {
		dataProvider.addAuthor(newdAuthor.getFullName(), newdAuthor.getEmail(), newdAuthor.getBirthday());
		this.hideDialog();
		RequestContext.getCurrentInstance().update("authors_list");
	}
	public void hideDialog() {
         RequestContext.getCurrentInstance().execute("createdlg.hide()");
	}
	
	public void fullNameValidate(FacesContext context, UIComponent component,Object value)
	{
		String name = (String)value;
		if (name==null)
		{
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Value is required",null));
		}
		if (!dataProvider.checkFullName(name))
		{
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"This author already is present",null));
		}
	}
	public void emailValidate(FacesContext context, UIComponent component,Object value)
	{
          String email = (String)value;
          if (!Pattern.compile(".+@.+\\..+").matcher(email).matches())
          {
			throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,"Bad email format",null));		
          }
	}

	public void setFullName(String name) {
		newdAuthor.setFullName(name);
	}

	public void setEmail(String email) {
		newdAuthor.setEmail(email);
	}
	
	public String getFullName() {
		return newdAuthor.getFullName();
	}

	public String getEmail() {
		return newdAuthor.getEmail();
	}
	
	public Date getBirthDay() {
		return newdAuthor.getBirthday();
	}

	public void setBirthDay(Date birthDay) {
		newdAuthor.setBirthday(birthDay);
	}

}
